package com.rz.core.kit;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class PwdKit {

    public static Map<String,String> createPwd(String pwd){
        Map<String,String> pwdMap = new HashMap<>(2);
        if(pwd==null || "".equals(pwd)){
            pwd = "123456";
        }
        String rdmSlat = getSlat();
        pwdMap.put("salt",rdmSlat);
        pwdMap.put("password",HashKit.md5(HashKit.md5(pwd) + rdmSlat));
        return pwdMap;
    }

    public static String getSlat() {
        Long salt = Math.round(Math.random() * 9000 + 1000);
        return salt.toString();
    }

    public static String getRealPwd(String pwd,String salt){
        return HashKit.md5(HashKit.md5(pwd) + salt);
    }

    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }

    public static String getToken(){
        return generateString(6)+"_"+generateString(12);
    }

    public static void main(String[] args) {
        Map<String,String> map = createPwd("admin123456");
        System.out.println(map.get("password"));
        System.out.println(map.get("salt"));
    }
}
